﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Admin.Common;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using Beika.Web.Common.Frame;

namespace FortuneHeaven.Admin.Pages.Auth
{
    public partial class RoleDefineDetail : PageBase
    {
        public override string PageID
        {
            get { return "F42"; }
        }
        private string pre = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitalAuthTree();
                if (ID >= 0)
                {
                    RoleBO bo = new RoleBO();
                    Role g = bo.GetInfo(ID);
                    this.txtName.Text = g.Name;
                    this.txtDescription.Text = g.Description;
                }
            }
        }

        public new int ID
        {
            get
            {
                return string.IsNullOrEmpty(Request["ID"]) ? -1 : int.Parse(Request["ID"]);
            }
        }

        public string Pre
        {
            get
            {
                if (!string.IsNullOrEmpty(pre))
                    return pre;
                else
                    pre = Server.UrlDecode(Request["pre"]);
                return pre;
            }
        }

        public string CheckStatus()
        {
            if (ID < 0)
            {
                return "新增角色";
            }
            else
            {
                return "修改角色";
            }
        }

        public void InitalAuthTree()
        {
            if (AuthorityCenter.Structure != null)
            {
                this.authTree.Nodes.Clear();
                TreeNode root = new TreeNode();
                root.Text = "目录";
                root.ShowCheckBox = true;
                this.authTree.Nodes.Add(root);
                TreeNode tempModule;
                TreeNode tempFunc;
                RoleAuth roleauth=  AuthorityCenter.GetAuth(ID);
                foreach (Module mo in roleauth.Modules)
                {
                    tempModule = new TreeNode(mo.Name, mo.ModuleNO)
                    {
                        ShowCheckBox = true
                    };
                    root.ChildNodes.Add(tempModule);
                    foreach (Function fu in mo.Functions)
                    {
                        tempFunc = new TreeNode(fu.Name, fu.FunctionNO)
                        {
                            ShowCheckBox = true
                        };
                        tempFunc.Checked = fu.IsAuth;
                        tempModule.ChildNodes.Add(tempFunc);
                    }
                }
            }
        }


        public void Save(object sender, EventArgs arg)
        {
            if (!this.IsValid)
            {
                return;
            }

            RoleBO bo = new RoleBO();
            Role g;
            if (ID >= 0)
            {
                g = bo.GetInfo(ID);

            }
            else
            {
                g = new Role();
                g.Time = System.DateTime.Now;
            }
            g.Name = this.txtName.Text;

            g.Description = this.txtDescription.Text;

            List<string> authority = new List<string>();
            foreach (TreeNode mo in this.authTree.Nodes[0].ChildNodes)
            {

                foreach (TreeNode fu in mo.ChildNodes)
                {
                    if (fu.Checked)
                    {
                        authority.Add(fu.Value);
                    }
                }
            }
            g.Auth = AuthorityCenter.SetAuthFunc(authority);
            if (ID >= 0)
            {
                bo.Save(g, false);
                Response.Write(string.Format("<Html> <script>alert('修改成功');document.location ='{0}'; </script> </HTML>", Pre));

            }
            else
            {
                g.ID = bo.GetMaxID();
                bo.Save(g, true);
                Response.Write(string.Format("<Html> <script>alert('新增成功');document.location ='{0}'; </script> </HTML>", Pre));
                Response.Flush();
            }
        }
        public void CancelSave(object sender, EventArgs arg)
        {
            Response.Redirect(Pre);
        }
    }
}